package com.rytong.tools.ui.jsfunction;

import java.io.IOException;

import org.keplerproject.luajava.LuaException;

import android.R.integer;
import android.app.Activity;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnDismissListener;
import android.content.res.AssetFileDescriptor;
import android.content.res.AssetManager;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnPreparedListener;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import com.rytong.tools.ui.Component;
import com.rytong.tools.utils.Utils;

public class EmpVideo implements OnPreparedListener, SurfaceHolder.Callback {

    private static final String TAG = "EmpVideo";
    private Dialog dialog_;
    private SurfaceView surfaceView_;
    private SurfaceHolder holder_;
    private MediaPlayer mediaPlayer_;
    private AssetFileDescriptor afd_;

    final String UNKNOWN_ERROR = "1";
    final String NO_FILE_ERROR = "2";
    final String PLAY_FAIL_ERROR = "3";
    final String PAUSE_FAIL_ERROR = "4";
    final String RESUME_FAIL_ERROR = "5";
    final String STOP_FAIL_ERROR = "6";

    /**
     * 加载视频文件
     * 
     * @param filename
     *            文件名称，如果以“/”开头则连接后台服务器下载，否则全部都在本地包内查找具有相同名称的文件。
     * @return 视频文件对象（发生错误则返回nil）
     */
    public Object load(Object filename) {
        if (filename instanceof String) {
            Activity activity = Utils.getActivity();
            AssetManager assets = activity.getAssets();
            String name = String.valueOf(filename);
            surfaceView_ = new SurfaceView(activity);
            holder_ = surfaceView_.getHolder();
            holder_.setFixedSize(200, 200);
            holder_.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
            holder_.addCallback(this);
            dialog_ = new Dialog(activity, Utils.getResourcesId(activity, "Dialog_Fullscreen", "style"));
            dialog_.setContentView(surfaceView_);
            dialog_.setOnDismissListener(new OnDismissListener() {

                @Override
                public void onDismiss(DialogInterface dialog) {
                    if (mediaPlayer_ != null) {
                        mediaPlayer_.release();
                    }
                }
            });
            try {
                if (name.startsWith("/")) {

                } else {
                    String[] list = assets.list("video");
                    String str = "";
                    int size = list.length;
                    for (int i = 0; i < size; i++) {
                        str = list[i];
                        if (str.equals(name)) {
                            mediaPlayer_ = new MediaPlayer();
                            afd_ = assets.openFd("video/" + name);
                            mediaPlayer_.setDataSource(afd_.getFileDescriptor(), afd_.getStartOffset(),
                                    afd_.getLength());
                            afd_.close();
                            afd_ = null;
                            mediaPlayer_.setOnPreparedListener(EmpVideo.this);
                            return mediaPlayer_;
                        }
                    }
                }
            } catch (IOException e) {
                Utils.printException(e);
            }
        }
        return mediaPlayer_;
    }

    /**
     */
    public void play() {
        dialog_.show();
    }

    /**
     */
    public void stop() {
        mediaPlayer_.stop();
        if (dialog_ != null && dialog_.isShowing()) {
            dialog_.dismiss();
        }
    }

    public void resume() {
        mediaPlayer_.start();
    }

    /**
     */
    public void pause() {
        mediaPlayer_.pause();
    }

    /**
     */
    public void dispose() {
        mediaPlayer_.release();
    }

    @Override
    public void onPrepared(MediaPlayer mp) {
        Utils.printOutToConsole("onprepared ");
        mp.start();
    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        Utils.printOutToConsole("surfaceCreated");
        mediaPlayer_.setDisplay(holder);
        mediaPlayer_.prepareAsync();
    }

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
        Utils.printOutToConsole("surfaceChanged");
    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        Utils.printOutToConsole("surfaceDestroyed");
    }
}